package com.ruoyi.prod.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.prod.entity.dto.ProdPurchaseDTO;
import com.ruoyi.prod.entity.po.ProdPurchase;
import com.ruoyi.prod.entity.query.ProdPurchaseCondition;
import com.ruoyi.prod.enums.OddTypeEnum;
import com.ruoyi.prod.mapper.ProdPurchaseMapper;
import com.ruoyi.prod.service.ProdPurchaseService;
import com.ruoyi.prod.util.OddUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 采购管理-采购计划 服务实现类
 * </p>
 *
 * @author 强子初
 * @since 2023-02-17
 */
@Service
public class ProdPurchaseServiceImpl extends ServiceImpl<ProdPurchaseMapper, ProdPurchase> implements ProdPurchaseService {

    @Override
    public String getOdd() {
        return OddUtil.generate(OddTypeEnum.PURCHASE_PLAN.getCode());
    }

    @Override
    public ProdPurchaseDTO getProcess(String id) {
        return baseMapper.getProcess(id);
    }

    @Override
    public List<ProdPurchase> list(ProdPurchase condition) {
        return this.list(this.getWrapper(condition));
    }

    @Override
    public Page<ProdPurchase> page(ProdPurchase condition) {
        Page<ProdPurchase> page = PageHelper.startPage(condition.getPageNum(), condition.getPageSize());
        List<ProdPurchase> list = this.list(condition);
        return page;
    }

    @Override
    public Page<ProdPurchaseDTO> page(ProdPurchaseCondition condition) {
        Page<ProdPurchaseDTO> page = PageHelper.startPage(condition.getPageNum(), condition.getPageSize());
        List<ProdPurchaseDTO> list = baseMapper.list(condition);
        return page;
    }

    private LambdaQueryWrapper<ProdPurchase> getWrapper(ProdPurchase condition) {
        LambdaQueryWrapper<ProdPurchase> wrapper = new LambdaQueryWrapper<>();

        if (condition.getStateList() != null) {
            wrapper.in(ProdPurchase::getAuditState, condition.getStateList());
        }
        return wrapper;
    }
}
